package com.ruoyi.project.check.controller;

import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.check.domain.HisCheckList;
import com.ruoyi.project.check.service.HisUploadService;
import io.lettuce.core.ScriptOutputType;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("/check")
public class HisFileUploadController {
    @Autowired
    private HisUploadService hisUploadService;

    /**
     * 上传检查结果
     * @param hisCheckList 检查结果图片地址已经检查结果文字描述
     * @return
     */
    @PostMapping("/uploadResult")
    public int uploadResult(@RequestBody HisCheckList hisCheckList){
        System.out.println(hisCheckList);
        return hisUploadService.uploadResult(hisCheckList);
    }

    /**
     * 检查结果图片回显
     * @param imageName 检查结果图片地址
     * @param response 响应流
     */
    @GetMapping("/imageName")
    public void showImage(String imageName, HttpServletResponse response){
        System.out.println("====================================访问成功");
        System.out.println(imageName+"==============================================ok");
        byte[] datas=hisUploadService.getImage(imageName);
        if (datas != null) {
            try {
                response.setContentType("application/octet-stream");
                response.getOutputStream().write(datas,0,datas.length);
                response.getOutputStream().flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 图片上传触发
     * @param file 图片
     * @return
     */
    @RequestMapping("/upload")
    public AjaxResult upload(MultipartFile file){
        System.out.println(file.getOriginalFilename());
        return AjaxResult.success(hisUploadService.uploadFile(file));
        //return hisUploadService.uploadFile(file);
    }
}
